import axios from 'axios'
import qs from 'qs'
axios.defaults.timeout = 20000
import {Dialog} from 'vant'
// import {parseParam} from '@/assets/js/utils';
// import VueCookies from 'vue-cookies';
import router from '@/router.js';

const parseParam = (obj) => {
	if(obj!={}){
		let result = '';
		let item;
		for (item in obj) {
			if ((obj[item] && String(obj[item]))||obj[item]===0) {
			  result += `&${item}=${obj[item]}`;
			}
		}
		if (result) {
			result = '?' + result.slice(1);
		}
		return result;
	}else{
		return ''
	}
};
// axios.defaults.headers.post['Content-Type'] = 'application/x-www=form-urlencoded'
// axios.interceptors.request.use(config => {
//   console.log("config",config)
//   //return config
// }, e => {
  
// })
export default {
  async fetchGet(url, params = {}) {
    return await new Promise((resolve, reject) => {
      // let token = VueCookies.get('token')
      // if (token) { 
      //     axios.defaults.headers.common["token"] = token
      // }
	  const token = localStorage.getItem(process.env.VUE_APP_TOKEN_NAME)
	  // axios.defaults.headers.common["sc-token"] = localStorage.getItem(process.env.VUE_APP_TOKEN_NAME);
	  axios.defaults.headers.common[process.env.VUE_APP_TOKEN_NAME] = token
	  url = url + parseParam(params)
      axios.get(url, params).then(res => {
		this.againLogin(res.data)
        resolve(res.data)
      }).catch(error => {
        reject(error)
      })
    })
  },
  async fetchGetNotToken(url, params = {}) {
    return await new Promise((resolve, reject) => {
  	  url = url + parseParam(params)
      axios.get(url, params).then(res => {
        resolve(res.data)
      }).catch(error => {
        reject(error)
      })
    })
  },
  async fetchPost4NoMsg(url, data = {}) {
    return await new Promise((resolve, reject) => {
      axios.post(url, qs.stringify(data)).then(res => {
  		 if(res.data.code!=200){
  			 // Dialog({ title:'温馨提示', message: res.data.msg, closeOnClickOverlay:true });
			 // router.replace('/login');
  		 }
        resolve(res.data)
      }).catch(error => {
  		  // Dialog({ title:'温馨提示', message: error, closeOnClickOverlay:true });
		  localStorage.setItem('matchResult',0)
          reject(error)
      })
    })
  },
  async fetchPost4login(url, data = {}) {
    return await new Promise((resolve, reject) => {
      axios.post(url, qs.stringify(data)).then(res => {
  		 if(res.data.code!=200){
  			 Dialog({ title:'温馨提示', message: res.data.msg, closeOnClickOverlay:true });
  		 }
        resolve(res.data)
      }).catch(error => {
  		  // Dialog({ title:'温馨提示', message: error, closeOnClickOverlay:true });
          reject(error)
      })
    })
  },
  async fetchPost(url, data = {}) {
    return await new Promise((resolve, reject) => {
	  const token = localStorage.getItem(process.env.VUE_APP_TOKEN_NAME)
	  axios.defaults.headers.common[process.env.VUE_APP_TOKEN_NAME] = token
      axios.post(url, qs.stringify(data)).then(res => {
		 if(res.data.code!=200){
			//  Dialog({ title:'温馨提示', message: res.data.msg.split('：')[1]||res.data.msg });
			Dialog({ title:'温馨提示', message: res.data.msg, closeOnClickOverlay:true });
		 }
		 this.againLogin(res.data)
		 // if(res.data.code==3){
			//  router.replace('/login');
		 // }
        resolve(res.data)
      }).catch(error => {
		  // Dialog({ title:'系统提示', message: error });
          reject(error)
      })
    })
  },
  fetchPostForUpload(url, data = {}) {
    return new Promise((resolve, reject) => {
      let config = {
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      } // 添加请求头
      axios.post(url, data, config).then(res => {
        resolve(res.data)
      }).catch(error => {
        reject(error)
      })
    })
  },
  againLogin(result){
      if(result.code == 100){
		localStorage.removeItem(process.env.VUE_APP_TOKEN_NAME)
		const redirect = location.href.split('#')[1]
		router.replace(`/login?redirect=${redirect}`);
      }
  }
}